package cn.aiseminar.aisinterphone;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class DeviceAdapter extends BaseAdapter {
	
	private ArrayList<Device> deviceData = null;	
	private static LayoutInflater inflater = null;
	
	public DeviceAdapter(Context context, ArrayList<Device> data)
	{
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		deviceData = data;
	}
	
	public void setDeviceData(ArrayList<Device> deviceData) {
		this.deviceData = deviceData;
		this.notifyDataSetChanged();
	}
	
	public void addDevice(Device dev) {
		if (null == deviceData)
		{
			deviceData = new ArrayList<Device>();
		}
		
		if (null != dev)
		{
			deviceData.add(dev);
		}
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (null == deviceData)
		{
			return 0;
		}
		return deviceData.size();
	}

	@Override
	public Object getItem(int position) {
		return deviceData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView)
		{
			convertView = inflater.inflate(R.layout.device_list_item, null);
		}
		
		TextView deviceNameView = (TextView)convertView.findViewById(R.id.deviceName);
		TextView deviceMacView = (TextView)convertView.findViewById(R.id.deviceMac);
		
		Device dev = deviceData.get(position);		
		deviceNameView.setText(dev.getName());
		deviceMacView.setText(dev.getMacAddress());
		
		return convertView;
	}
}
